﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using System.Data;
using MLY;

namespace MLY.Reports
{
    public partial class reportsViewer : MLY.Bll.BasePage
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request.QueryString["repinfo"] != null)
                _reportKey = (string)Request.QueryString["repinfo"];
            if (!string.IsNullOrEmpty(_reportKey))
                _repInfo = MLY.Bll.ReportInfo.GetReportInfo(_reportKey);
            if (_repInfo != null)
            {
                _userRole = Request.QueryString["userRole"].ToString();
                if (_repInfo.ReportType < 1000)
                {
                    if (_userRole == "Client")
                    {
                        ClientsReports();
                    }
                    else if (_userRole == "Contractor")
                    {
                        ContractorsReports();
                    }
                }
                else
                {
                    ShowAdvanceReport();
                }
            }
            else
            {
                CrystalReportViewer1.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnError(EventArgs e)
        {
            base.OnError(e);
        }

        protected void CrystalReportViewer1_Unload(object sender, EventArgs e)
        {
            //if (rpt1 != null)
            //{
            //    rpt1.Close();
            //    rpt1.Dispose();
            //}
            //if (rpt2 != null)
            //{
            //    rpt2.Close();
            //    rpt2.Dispose();
            //}
            //if (rpt3 != null)
            //{
            //    rpt3.Close();
            //    rpt3.Dispose();
            //}
            //if (rpt4 != null)
            //{
            //    rpt4.Close();
            //    rpt4.Dispose();
            //}
            //if (rpt5 != null)
            //{
            //    rpt5.Close();
            //    rpt5.Dispose();
            //}
            //if (rpt6 != null)
            //{
            //    rpt6.Close();
            //    rpt6.Dispose();
            //}
            //if (rpt7 != null)
            //{
            //    rpt7.Close();
            //    rpt7.Dispose();
            //}
        }
        
        protected void btnClose_Click(object sender, EventArgs e)
        {
            MLY.Bll.ReportInfo.RemoveReportInfo(_reportKey);
            Response.Write("<script language='javascript'>window.close ();</script>");
        }

        protected void ContractorsReports()
        {
            switch (_repInfo.ReportType)
            {
                case 1: 
                    // Inventory Report
                    //ContractorsReports.rptInventory rpt1 = new MLY.Reports.ContractorsReports.rptInventory();
                    dsReportsMLY.TableWorkOrdersDataTable dt1 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da1 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    da1.FillInventoryByOrgIdContractor(dt1, _repInfo.SelectedOrganizationId, _repInfo.OrganizationId);
                    //rpt1.SetDataSource((DataTable)dt1);
                    //CrystalReportViewer1.ReportSource = rpt1;
                    break;
                case 2: 
                    // Opened Late Work Orders Report
                    //ContractorsReports.rptOpenedLateWorkOrders rpt2 = new ContractorsReports.rptOpenedLateWorkOrders();
                    dsReportsMLY.TableWorkOrdersDataTable dt2 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da2 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    string byPlanned = (_repInfo.DateColumn == "PlannedDelivaryDate" ? "1" : "0");
                    da2.FillOpenLateWOContractor(dt2, _repInfo.SelectedOrganizationId, _repInfo.OrganizationId, byPlanned);
                    //rpt2.SetDataSource((DataTable)dt2);
                    //CrystalReportViewer1.ReportSource = rpt2;
                    break;
                case 3: 
                    // details report
                    //ContractorsReports.rptOrganizationDetails rpt3 = new ContractorsReports.rptOrganizationDetails();
                    dsReportsMLY.TableOrganizationDataTable dt3 = new dsReportsMLY.TableOrganizationDataTable();
                    dsReportsMLYTableAdapters.TableOrganizationTableAdapter da3 = new MLY.Reports.dsReportsMLYTableAdapters.TableOrganizationTableAdapter();
                    da3.FillCustomersDetailsForContractor(dt3, _repInfo.SelectedOrganizationId, _repInfo.OrganizationId);
                    //rpt3.SetDataSource((DataTable)dt3);
                    //CrystalReportViewer1.ReportSource = rpt3;
                    break;
                case 4: 
                    // Work Orders By Dates
                    //ContractorsReports.rptWorkOrdersByDates rpt4 = new ContractorsReports.rptWorkOrdersByDates();
                    dsReportsMLY.TableWorkOrdersDataTable dt4 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da4 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    string Where = string.Format(@" ((TableWorkOrder.CustomerId = {0})OR({0}=0)) AND ((TableWorkOrder.ContractorId = {1}))", _repInfo.SelectedOrganizationId, _repInfo.OrganizationId);
                    if (!string.IsNullOrEmpty(_repInfo.DateColumn) && _repInfo.DateFrom != null && _repInfo.DateTill != null)
                    {
                        Where += string.Format(@" And TableWorkOrder.{0} ", _repInfo.DateColumn);
                        Where += string.Format(@" Between {0} And {1} ", Utils.FormatDateToSql(_repInfo.DateFrom), 
                                                                         Utils.FormatDateToSql(_repInfo.DateTill));
                    }
                    Where += string.Format(@" AND Quantity > 0 ");
                    string orderby = "OrganizationName, " + _repInfo.DateColumn;
                    da4.FillByWhere(dt4, Where, orderby, false);
                    //rpt4.SetDataSource((DataTable)dt4);
                    //rpt4.SetParameterValue("ColumnFilterName", _repInfo.DateColumn);
                    //rpt4.SetParameterValue("FromDate", ((DateTime)_repInfo.DateFrom).ToString("dd/MM/yyyy"));
                    //rpt4.SetParameterValue("ToDate", ((DateTime)_repInfo.DateTill).ToString("dd/MM/yyyy"));
                    //CrystalReportViewer1.ReportSource = rpt4;
                    break;
                case 5: 
                    // ERRORS lOG
                    //ClientsReports.rptErrLog rpt5 = new ClientsReports.rptErrLog();
                    dsReportsMLY.logTblDataTable dt5 = new dsReportsMLY.logTblDataTable();
                    dsReportsMLYTableAdapters.logTblTableAdapter da5 = new MLY.Reports.dsReportsMLYTableAdapters.logTblTableAdapter();
                    string Where5 = "";
                    if (_repInfo.DateFrom != null)
                    {
                        Where5 += @" (isError = 1)";
                        Where5 += string.Format(@" AND timeReceived Between {0} And {1} ", Utils.FormatDateToSql(_repInfo.DateFrom), Utils.FormatDateToSql(_repInfo.DateTill));
                    }
                    da5.FillByWhere(dt5, Where5, "");
                    //rpt5.SetDataSource((DataTable)dt5);
                    //rpt5.SetParameterValue("DateFrom", ((DateTime)_repInfo.DateFrom).ToString("dd/MM/yyyy"));
                    //rpt5.SetParameterValue("DateTo", ((DateTime)_repInfo.DateTill).ToString("dd/MM/yyyy"));
                    //CrystalReportViewer1.ReportSource = rpt5;
                    break;
                case 6: 
                    // Canceled Work Orders
                    //ClientsReports.rptCanceledWorkOrders rpt6 = new MLY.Reports.ClientsReports.rptCanceledWorkOrders();
                    dsReportsMLY.TableCanceledWoDataTable dt6 = new dsReportsMLY.TableCanceledWoDataTable();
                    dsReportsMLYTableAdapters.TableCanceledWoTableAdapter da6 = new MLY.Reports.dsReportsMLYTableAdapters.TableCanceledWoTableAdapter();
                    da6.FillCancelledWoContractor(dt6, _repInfo.OrganizationId);
                    //rpt6.SetDataSource((DataTable)dt6);
                    //CrystalReportViewer1.ReportSource = rpt6;
                    break;

                case 7: 
                    // Late Work Orders
                    //ContractorsReports.rptLateWorkOrders rpt7 = new ContractorsReports.rptLateWorkOrders();
                    dsReportsMLY.TableWorkOrdersDataTable dt7 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da7 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    da7.FillLateWOContractor(dt7, _repInfo.SelectedOrganizationId, _repInfo.OrganizationId);
                    //rpt7.SetDataSource((DataTable)dt7);
                    //CrystalReportViewer1.ReportSource = rpt7;
                    break;
                default:
                    break;
            }
        }

        protected void ClientsReports()
        {
            switch (_repInfo.ReportType)
            {
                case 1: 
                    // Inventory Report
                    //ClientsReports.rptInventory rpt1 = new ClientsReports.rptInventory();
                    dsReportsMLY.TableWorkOrdersDataTable dt1 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da1 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    da1.FillInventoryByOrgIdCustomer(dt1, _repInfo.OrganizationId, _repInfo.SelectedOrganizationId);
                    //rpt1.SetDataSource((DataTable)dt1);
                    //CrystalReportViewer1.ReportSource = rpt1;
                    break;
                case 2:
                    // Opened Late Work Orders
                    //ClientsReports.rptOpenedLateWorkOrders rpt2 = new ClientsReports.rptOpenedLateWorkOrders();
                    dsReportsMLY.TableWorkOrdersDataTable dt2 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da2 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    string byPlanned = (_repInfo.DateColumn == "PlannedDelivaryDate" ? "1" : "0");
                    da2.FillOpenLateWOCustomers(dt2, _repInfo.OrganizationId, _repInfo.SelectedOrganizationId, byPlanned);
                    //rpt2.SetDataSource((DataTable)dt2);
                    //CrystalReportViewer1.ReportSource = rpt2;
                    break;
                case 3:
                    // Details Report
                    //ClientsReports.rptOrganizationDetails rpt3 = new ClientsReports.rptOrganizationDetails();
                    dsReportsMLY.TableOrganizationDataTable dt3 = new dsReportsMLY.TableOrganizationDataTable();
                    dsReportsMLYTableAdapters.TableOrganizationTableAdapter da3 = new MLY.Reports.dsReportsMLYTableAdapters.TableOrganizationTableAdapter();
                    da3.FillContractorsDetailsForClient(dt3, _repInfo.OrganizationId, _repInfo.SelectedOrganizationId);
                    //rpt3.SetDataSource((DataTable)dt3);
                    //CrystalReportViewer1.ReportSource = rpt3;
                    break;
                case 4:
                    // Work Orders By Dates
                    //ClientsReports.rptWorkOrdersByDates rpt4 = new ClientsReports.rptWorkOrdersByDates();
                    dsReportsMLY.TableWorkOrdersDataTable dt4 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da4 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    string Where = string.Format(@" ((TableWorkOrder.CustomerId = {0})) AND ((TableWorkOrder.ContractorId = {1})OR({1}=0))", _repInfo.OrganizationId, _repInfo.SelectedOrganizationId);
                    if ((_repInfo.DateColumn != null) && (_repInfo.DateFrom != null) && (_repInfo.DateTill != null))
                    {
                        Where += string.Format(@" And TableWorkOrder.{0} ", _repInfo.DateColumn);
                        Where += string.Format(@" Between {0} And {1} ", Utils.FormatDateToSql(_repInfo.DateFrom), 
                                                                         Utils.FormatDateToSql(_repInfo.DateTill));
                    }
                    Where += string.Format(@" AND Quantity > 0 ");
                    string orderby = "OrganizationName, " + _repInfo.DateColumn;
                    da4.FillByWhere(dt4, Where, orderby, true);
                    //rpt4.SetDataSource((DataTable)dt4);
                    //rpt4.SetParameterValue("ColumnFilterName", _repInfo.DateColumn);
                    //rpt4.SetParameterValue("FromDate", ((DateTime)_repInfo.DateFrom).ToString("dd/MM/yyyy"));
                    //rpt4.SetParameterValue("ToDate", ((DateTime)_repInfo.DateTill).ToString("dd/MM/yyyy"));
                    //CrystalReportViewer1.ReportSource = rpt4;
                    break;
                case 5:
                    // Errors Report
                    //ClientsReports.rptErrLog rpt5 = new ClientsReports.rptErrLog();
                    dsReportsMLY.logTblDataTable dt5 = new dsReportsMLY.logTblDataTable();
                    dsReportsMLYTableAdapters.logTblTableAdapter da5 = new MLY.Reports.dsReportsMLYTableAdapters.logTblTableAdapter();
                    string Where5 = "";
                    if (_repInfo.DateFrom != null && _repInfo.DateTill != null)
                    {
                        Where5 += @" (isError = 1)";
                        Where5 += string.Format(@" AND timeReceived Between {0} And {1} ", Utils.FormatDateToSql(_repInfo.DateFrom), Utils.FormatDateToSql(_repInfo.DateTill));
                    }

                    da5.FillByWhere(dt5, Where5, "");
                    //rpt5.SetDataSource((DataTable)dt5);
                    //rpt5.SetParameterValue("DateFrom", ((DateTime)_repInfo.DateFrom).ToString("dd/MM/yyyy"));
                    //rpt5.SetParameterValue("DateTo", ((DateTime)_repInfo.DateTill).ToString("dd/MM/yyyy"));
                    //CrystalReportViewer1.ReportSource = rpt5;
                    break;
                case 6:
                    // Canceled Work Orders
                    //ClientsReports.rptCanceledWorkOrders rpt6 = new MLY.Reports.ClientsReports.rptCanceledWorkOrders();
                    dsReportsMLY.TableCanceledWoDataTable dt6 = new dsReportsMLY.TableCanceledWoDataTable();
                    dsReportsMLYTableAdapters.TableCanceledWoTableAdapter da6 = new MLY.Reports.dsReportsMLYTableAdapters.TableCanceledWoTableAdapter();
                    da6.FillCanceledWorkOrders(dt6, _repInfo.OrganizationId);
                    //rpt6.SetDataSource((DataTable)dt6);
                    //CrystalReportViewer1.ReportSource = rpt6;
                    break;
                case 7:
                    // Late Work Orders Report
                    //ClientsReports.rptLateWorkOrders rpt7 = new ClientsReports.rptLateWorkOrders();
                    dsReportsMLY.TableWorkOrdersDataTable dt7 = new dsReportsMLY.TableWorkOrdersDataTable();
                    dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter da7 = new MLY.Reports.dsReportsMLYTableAdapters.TableWorkOrdersTableAdapter();
                    da7.FillLateWOCustomer(dt7, _repInfo.OrganizationId, _repInfo.SelectedOrganizationId);
                    //rpt7.SetDataSource((DataTable)dt7);
                    //CrystalReportViewer1.ReportSource = rpt7;
                    break;
                default:
                    break;
            }
        }
        
        private void ShowAdvanceReport()
        {
            MLY.Bll.clsOrganization org = new MLY.Bll.clsOrganization(_repInfo.OrganizationId);
            try
            {
                switch (_repInfo.ReportType)
                {
                    case 1001:
                        // OTD Report
                        string Where2 = string.Format(@" And ((ActualDelivaryDate <> '01/01/2099') OR (ActualDelivaryDate is NULL ))");
                        Where2 += string.Format(" AND (dbo.TableWorkOrder.RequestedDelivaryDate is not null) ");
                        Where2 += string.Format(" AND (Quantity > 0) ");
                        if ((_repInfo.DateColumn != null) && (_repInfo.DateFrom != null) && (_repInfo.DateTill != null))
                        {
                            Where2 += string.Format(@" And (TableWorkOrder.{0} ", _repInfo.DateColumn);
                            Where2 += string.Format(@" Between {0} And {1}) ", Utils.FormatDateToSql(_repInfo.DateFrom), Utils.FormatDateToSql(_repInfo.DateTill));
                        }
                        if ((bool)org.Data.IsClient)
                        {
                            // Change here
                            //AdvancedReports.rptOTD_Contractors rpt1 = new MLY.Reports.AdvancedReports.rptOTD_Contractors();
                            MLY.Reports.AdvancedReports.dsAdvancedRpt.TableOTDContractorsDataTable dt1 = new MLY.Reports.AdvancedReports.dsAdvancedRpt.TableOTDContractorsDataTable();
                            MLY.Reports.AdvancedReports.dsAdvancedRptTableAdapters.TableOTDContractorsTableAdapter da1 = new MLY.Reports.AdvancedReports.dsAdvancedRptTableAdapters.TableOTDContractorsTableAdapter();
                            string Where1 = string.Format(@"  (dbo.TableWorkOrder.CustomerId  =  {0}) ", org.Data.OrganizationId);
                            if (_repInfo.SelectedOrganizationId != 0)
                            {
                                Where1 += string.Format(@" AND (dbo.TableWorkOrder.ContractorId =  {0}) ", _repInfo.SelectedOrganizationId);
                            }
                            da1.FillByWhere(dt1, Where1 + Where2, "");
                            //rpt1.SetDataSource((DataTable)dt1);
                            //rpt1.SetParameterValue("ColumnFilterName", _repInfo.DateColumn);
                            //rpt1.SetParameterValue("FromDate", ((DateTime)_repInfo.DateFrom).ToString("dd/MM/yyyy"));
                            //rpt1.SetParameterValue("ToDate", ((DateTime)_repInfo.DateTill).ToString("dd/MM/yyyy"));
                            //rpt1.SetParameterValue("CustomerName", org.Data.OrganizationName);
                            //CrystalReportViewer1.ReportSource = rpt1;
                        }
                        else if ((bool)org.Data.IsContractor)
                        {
                            //AdvancedReports.rptOTD_Customers rpt1 = new MLY.Reports.AdvancedReports.rptOTD_Customers();
                            MLY.Reports.AdvancedReports.dsAdvancedRpt.TableOTDCustomersDataTable dt1 = new MLY.Reports.AdvancedReports.dsAdvancedRpt.TableOTDCustomersDataTable();
                            MLY.Reports.AdvancedReports.dsAdvancedRptTableAdapters.TableOTDCustomersTableAdapter da1 = new MLY.Reports.AdvancedReports.dsAdvancedRptTableAdapters.TableOTDCustomersTableAdapter();
                            string Where1 = string.Format(@" (dbo.TableWorkOrder.ContractorId =  {0})  ", org.Data.OrganizationId);
                            if (_repInfo.SelectedOrganizationId != 0)
                            {
                                Where1 += string.Format(@" AND dbo.TableWorkOrder.CustomerId  =  {0} ", _repInfo.SelectedOrganizationId);
                            }
                            da1.FillByWhere(dt1, Where1 + Where2, "");
                            //rpt1.SetDataSource((DataTable)dt1);
                            //rpt1.SetParameterValue("ColumnFilterName", _repInfo.DateColumn);
                            //rpt1.SetParameterValue("FromDate", ((DateTime)_repInfo.DateFrom).ToString("dd/MM/yyyy"));
                            //rpt1.SetParameterValue("ToDate", ((DateTime)_repInfo.DateTill).ToString("dd/MM/yyyy"));
                            //rpt1.SetParameterValue("ContractorName", org.Data.OrganizationName);
                            //CrystalReportViewer1.ReportSource = rpt1;
                        }
                        break;
                    case 1002:
                        // Tag Rating
                        //AdvancedReports.rptTagRating rpt2 = new MLY.Reports.AdvancedReports.rptTagRating();
                        MLY.Reports.AdvancedReports.dsAdvancedRpt.TableSearchTagRatingDataTable dt2 = MLY.Bll.clsOrganization.SelfRating(_repInfo.OrganizationId);
                        //rpt2.SetDataSource((DataTable)dt2);
                        //CrystalReportViewer1.ReportSource = rpt2;
                        break;
                    case 1003: 
                        // Average Work Orders Duration report
                        //AdvancedReports.rptAvgWoDuration rpt3 = new MLY.Reports.AdvancedReports.rptAvgWoDuration();
                        MLY.Reports.AdvancedReports.dsAdvancedRpt.TableAvgWoDurationDataTable dt3 = new MLY.Reports.AdvancedReports.dsAdvancedRpt.TableAvgWoDurationDataTable();
                        MLY.Reports.AdvancedReports.dsAdvancedRptTableAdapters.TableAvgWoDurationTableAdapter da3 = new MLY.Reports.AdvancedReports.dsAdvancedRptTableAdapters.TableAvgWoDurationTableAdapter();
                        string Where = "";
                        if ((bool)org.Data.IsClient)
                            Where = string.Format(@" CustomerId = {0} ", org.Data.OrganizationId);
                        else
                            Where = string.Format(@" ContractorId = {0} ", org.Data.OrganizationId);
                        Where += string.Format(@" And (ActualDelivaryDate <> '01/01/2099') ");
                        Where += string.Format(@" And (ActualDelivaryDate is not null) ");
                        Where += string.Format(@" And (Quantity > 0) ");
                        if (!string.IsNullOrEmpty(_repInfo.DateColumn))
                        {
                            Where += string.Format(@" And (TableWorkOrder.{0} ", _repInfo.DateColumn);
                            if ((_repInfo.DateFrom != null) && (_repInfo.DateTill != null))
                            {
                                Where += string.Format(@" Between {0} And {1}) ", Utils.FormatDateToSql(_repInfo.DateFrom), Utils.FormatDateToSql(_repInfo.DateTill));
                            }
                            else if ((!string.IsNullOrEmpty(_repInfo.ExpressionText)) && (!string.IsNullOrEmpty(_repInfo.ExpressionValue)))
                            {
                                Where += string.Format(@" {0} {1}) ", _repInfo.ExpressionText, _repInfo.ExpressionValue);
                            }
                            else if (!string.IsNullOrEmpty(_repInfo.ExpressionLike))
                            {
                                Where += string.Format(@" LIKE '{0}') ", _repInfo.ExpressionLike);
                            }
                        }
                        Where += " ";
                        da3.FillByWhere(dt3, Where, "", (bool)org.Data.IsClient);
                        //rpt3.SetDataSource((DataTable)dt3);
                        //CrystalReportViewer1.ReportSource = rpt3;
                        break;
                    case 1004:
                        // Quantity Per Month
                       // AdvancedReports.rptQuantityPerMonth2 rpt4 = new MLY.Reports.AdvancedReports.rptQuantityPerMonth2();
                        MLY.Reports.AdvancedReports.dsAdvancedRpt.TableQuantityPerMonth2DataTable dt4;
                        if (!string.IsNullOrEmpty(_repInfo.DateColumn))
                        {
                            dt4 = MLY.Bll.clsOrganization.GetQuantityPerMonthReport(_repInfo.OrganizationId, _repInfo.SelectedOrganizationId, _repInfo.DateColumn, (DateTime)_repInfo.DateFrom, (DateTime)_repInfo.DateTill);
                        }
                        else
                        {
                            dt4 = MLY.Bll.clsOrganization.GetQuantityPerMonthReport(_repInfo.OrganizationId, _repInfo.SelectedOrganizationId, "SendDate", (DateTime)_repInfo.DateFrom, (DateTime)_repInfo.DateTill);
                        }
                        //rpt4.SetDataSource((DataTable)dt4);
                        //CrystalReportViewer1.ReportSource = rpt4;
                        break;
                    case 1005:
                        // Inventory Balance Report
                        //AdvancedReports.rptInventoryBalanceDetailed rpt5 = new MLY.Reports.AdvancedReports.rptInventoryBalanceDetailed();
                        //AdvancedReports.rptInventoryBalance rpt5_ = new MLY.Reports.AdvancedReports.rptInventoryBalance();
                        MLY.Reports.AdvancedReports.dsAdvancedRpt.InventoryBalanceDetailsDataTable dt5;
                        if (!string.IsNullOrEmpty(_repInfo.DateColumn) &&
                            (_repInfo.DateColumn == "PlannedDelivaryDate"))
                        {
                            dt5 = MLY.Bll.clsWorkOrder.GetInventoryBalanceDetailsForReport(_repInfo.OrganizationId,
                                                                                        _repInfo.SelectedOrganizationId,
                                                                                        (DateTime)_repInfo.DateFrom,
                                                                                        (DateTime)_repInfo.DateTill);
                            //rpt5.SetDataSource((DataTable)dt5);
                            //CrystalReportViewer1.ReportSource = rpt5;
                        }
                        else
                        {
                            dt5 = MLY.Bll.clsWorkOrder.GetInventoryBalanceForReport(_repInfo.OrganizationId,
                                                                                        _repInfo.SelectedOrganizationId,
                                                                                        (DateTime)_repInfo.DateFrom,
                                                                                        (DateTime)_repInfo.DateTill);
                            //rpt5_.SetDataSource((DataTable)dt5);
                            //CrystalReportViewer1.ReportSource = rpt5_;
                        }
                        break;
                    case 1006:
                        // Graph Inventory Balance
                        //AdvancedReports.rptGraphInventoryBalance rpt6 = new MLY.Reports.AdvancedReports.rptGraphInventoryBalance();
                        MLY.Reports.AdvancedReports.dsAdvancedRpt.TableDeliveredQuantities2DataTable dt6;
                        if (!string.IsNullOrEmpty(_repInfo.DateColumn))
                        {
                            dt6 = MLY.Bll.clsOrganization.GetInventoryBalanceForReport(_repInfo.SelectedOrganizationId,
                                                                                        _repInfo.OrganizationId,
                                                                                        _repInfo.DateColumn,
                                                                                        (DateTime)_repInfo.DateFrom,
                                                                                        (DateTime)_repInfo.DateTill);
                        }
                        else
                        {
                            dt6 = MLY.Bll.clsOrganization.GetInventoryBalanceForReport(_repInfo.SelectedOrganizationId,
                                                                                        _repInfo.OrganizationId,
                                                                                        "RequestedDelivaryDate",
                                                                                        (DateTime)_repInfo.DateFrom,
                                                                                        (DateTime)_repInfo.DateTill);
                        }
                        //rpt6.SetDataSource((DataTable)dt6);
                        //CrystalReportViewer1.ReportSource = rpt6;
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CrystalReportViewer1.ReportSource = null;
                errLbl.Text = ex.Message;
            }
        }


        string _reportKey;
        MLY.Bll.ReportInfo _repInfo;
        string _userRole = "";

        //ClientsReports.rptInventory rpt1;
        //ClientsReports.rptOpenedLateWorkOrders rpt2;
        //ClientsReports.rptOrganizationDetails rpt3;
        //ClientsReports.rptWorkOrdersByDates rpt4;
        //ClientsReports.rptErrLog rpt5;
        //ClientsReports.rptCanceledWorkOrders rpt6;
        //ClientsReports.rptLateWorkOrders rpt7;


    }
}
